package com.ruoyi.client.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.client.domain.SysMedical;
import com.ruoyi.client.service.ISysMedicalService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 器械信息 Controller
 *
 * @author ruoyi
 * @date 2022-09-14
 */
@RestController
@RequestMapping("/system/medical")
public class SysMedicalController extends BaseController {
    @Autowired
    private ISysMedicalService sysMedicalService;

    /**
     * 查询器械信息 列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysMedical sysMedical) {
        startPage();
        List<SysMedical> list = sysMedicalService.selectSysMedicalList(sysMedical);
        return getDataTable(list);
    }

    /**
     * 导出器械信息 列表
     */
    @PreAuthorize("@ss.hasPermi('system:medical:export')")
    @Log(title = "器械信息 ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysMedical sysMedical) {
        List<SysMedical> list = sysMedicalService.selectSysMedicalList(sysMedical);
        ExcelUtil<SysMedical> util = new ExcelUtil<SysMedical>(SysMedical.class);
        util.exportExcel(response, list, "器械信息 数据");
    }

    /**
     * 获取器械信息 详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:medical:query')")
    @GetMapping(value = "/{medId}")
    public AjaxResult getInfo(@PathVariable("medId") Long medId) {
        return AjaxResult.success(sysMedicalService.selectSysMedicalByMedId(medId));
    }

    /**
     * 新增器械信息
     */
    @PreAuthorize("@ss.hasPermi('system:medical:add')")
    @Log(title = "器械信息 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysMedical sysMedical) {
        return toAjax(sysMedicalService.insertSysMedical(sysMedical));
    }

    /**
     * 修改器械信息
     */
    @PreAuthorize("@ss.hasPermi('system:medical:edit')")
    @Log(title = "器械信息 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysMedical sysMedical) {
        return toAjax(sysMedicalService.updateSysMedical(sysMedical));
    }

    /**
     * 删除器械信息
     */
    @PreAuthorize("@ss.hasPermi('system:medical:remove')")
    @Log(title = "器械信息 ", businessType = BusinessType.DELETE)
    @DeleteMapping("/{medIds}")
    public AjaxResult remove(@PathVariable Long[] medIds) {
        return toAjax(sysMedicalService.deleteSysMedicalByMedIds(medIds));
    }


    /**
     * 新增器械信息 列表
     */
    @GetMapping("/addInfo")
    public AjaxResult getInfo(SysMedical sysMedical) {
        sysMedical.setMedId(null);
//        查询是否有此类商品
        List<SysMedical> list = sysMedicalService.selectSysMedicalList(sysMedical);
//        判断商品是否存在
        if (list.isEmpty()) {
            sysMedicalService.insertSysMedical(sysMedical);
            List<SysMedical> list1 = sysMedicalService.selectSysMedicalList(sysMedical);
//            判断商品是否添加成功
            if (list1.isEmpty()){
//                未成功
                return toAjax(0);
            }else {
//                成功赋值id并且返回商品信息
                SysMedical med = list1.get(0);
                return AjaxResult.success(med);
            }
        }else {
//            有此类商品,赋值id,返回商品信息
            final SysMedical sysMedical1 = list.get(0);
            return AjaxResult.success(sysMedical1);
        }
    }
}
